﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EF.Model.Base;
using EF.Model;
using EF.BLL.Base;
using EF.Util;
using EF.Util.DBUtility;

/// <summary>
/// Copyright (C) 2010 峰予信息科技有限公司 
/// 版权所有
///  
/// 文件名：         Arg
/// 文件功能描述：   参数表新建,编辑,查看
/// 　
/// 创建标识：       darkross.zhou20100511 
/// 
/// </summary>
public partial class Trans_Base_Arg : BasePage
{
    private ArgService argBLL = new ArgService();

    int userId = 0; //用户Id
    string role = ""; //角色
    string lang = ""; //语言

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckSession();

        userId = Convert.ToInt32(Session["uid"].ToString()); //用户Id
        role = Session["role"].ToString(); //角色
        lang = Session["lang"].ToString(); //语言

        Response.Clear();

        switch (Request["action"].ToString().ToLower())
        {
            case "arg":
                GetArg();
                break;
            case "typelist":
                GetArgTypeList();
                break;
            case "add":
                Add();//1,新建成功;2,新建失败,参数标识已存在;0,新建失败,数据库异常.
                break;
            case "export": //导出
                Export();
                break;
            case "model": //查看
                GetModel();
                break;
            case "edit": //编辑
                Edit();
                break;
            case "logicdel": //1,逻辑删除成功;0,逻辑删除失败,数据库异常.
                LogicDel();
                break;
            case "arglist"://获取参数管理中的数据
                GetArgList();
                break;
            case "argmodel"://获取参数管理中的数据
                GetArgList();
                break;
            default:
                break;
        }

        Response.End();
    }

    private void GetArg()
    {
        try
        {   
            string argtype = Request["arg_type"].ToString();
            DataTable dt = argBLL.GetArg(argtype, lang);

            Response.Write("{success:true,rows:" + JsonHelp.WriteDataTable(dt) + "}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Export()
    {
        try
        {

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetArgTypeList()
    {
        try
        {
            int rowsCount; //总记录数

            int? pageSize = Request["limit"] == null ? Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"].ToString()) : Convert.ToInt32(Request["limit"].ToString());
            int? pageIndex = Request["start"] == null ? 1 : Convert.ToInt32(Request["start"].ToString()) / pageSize + 1;

            string sort = Request["sort"] == null ? "UpdateTime" : Request["sort"].ToString();
            string dir = Request["dir"] == null ? "DESC" : Request["dir"].ToString();

            string columns = "ArgTypeID,ArgTypeName_cn,ArgTypeName_en,ArgTypeName_ja,ArgTypeMark,SortIndex,CreateTime,UpdateTime"; //返回的列

            DataTable dt = argBLL.GetArgTypeList(lang,columns,pageIndex,pageSize,sort + " " + dir, out rowsCount);

            Response.Write("{success:true,rowsCount:" + rowsCount + ",rows:" + JsonHelp.WriteDataTable(dt) + "}");


        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Add()//1,新建成功;2,新建失败,参数标识已存在;0,新建失败,数据库异常.
    {
        try
        {
            ArgTypeModel model = (ArgTypeModel)JsonHelp.Convert2Entity(Request["model"].ToString(), typeof(ArgTypeModel));
            model.CreateUser = userId;
            string msg;
            int result = argBLL.Add(model,lang,out msg);
            Response.Write("{success:true,result:" + result + ",msg:'" + msg + "'}");

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetModel()
    {
        try
        {
            int selargtypeid = Convert.ToInt32(Request["sel_argtype_id"].ToString());
            ArgTypeModel model = argBLL.GetModel(lang, selargtypeid);

            Response.Write("{success:true,rows:" + JsonHelp.Convert2Json(model) + "}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Edit()//1,编辑成功;0,编辑失败,数据库异常.
    {
        try
        {
            ArgTypeModel model = (ArgTypeModel)JsonHelp.Convert2Entity(Request ["model"].ToString (),typeof(ArgTypeModel));
            model.UpdateUser = userId;
            string msg;
            Response.Write("{success:true,result:" + argBLL.Edit(model, lang, out msg) + ",msg:'" + msg + "'}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void LogicDel() //1,逻辑删除成功;0,逻辑删除失败,数据库异常.
    {
        try
        {
            string selArgTypeIds = Request["sel_argtype_id"].ToString();

            string msg;
            Response.Write("{success:true,result:" + argBLL.LogicDel(selArgTypeIds, userId, lang, out msg) + ",msg:'" + msg + "'}");

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetArgList()
    {
        try
        {
            int rowsCount; //总记录数

            int? pageSize = Request["limit"] == null ? Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"].ToString()) : Convert.ToInt32(Request["limit"].ToString());
            int? pageIndex = Request["start"] == null ? 1 : Convert.ToInt32(Request["start"].ToString()) / pageSize + 1;

            string sort = Request["sort"] == null ? "UpdateTime" : Request["sort"].ToString();
            string dir = Request["dir"] == null ? "DESC" : Request["dir"].ToString();

            string columns = "ArgId,ArgMark,ArgTypeId,ArgNameCn,ArgNameEn,ArgNameJa,Status,SortIndex,CreateUser,CreateTime,UpdateUser,UpdateTime"; //返回的列

            string sel_argtype_id = Request["sel_argtype_id"].ToString();

            DataTable dt = argBLL.GetArgList(lang, columns, pageIndex, pageSize, sort + " " + dir, out rowsCount, sel_argtype_id);

            Response.Write("{success:true,rowsCount:" + rowsCount + ",rows:" + JsonHelp.WriteDataTable(dt) + "}");


        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }
}
